<?php
namespace app\api\controller;

use lib\Db;
use lib\Response;

class Comment extends Common{

    /**
     * 评论列表
     * @return array
     */
    function commentList(){
        if(!is_numeric($this->post['article_id'])){
            return Response::error('获取文章ID失败!');
        }
        $replyIdMap = array();
        $list = Db::name('comment c')->join('user u', 'u.id = c.uid', 'left')
            ->limit($this->post['page'] * $this->post['pageRow'], $this->post['pageRow'])
            ->where(['article_id'=>$this->post['article_id']])->select();
        foreach ($list as $item){
            if(is_numeric($item['reply_id'])){
                array_push($replyIdMap, $item['id']);
            }
        }
        if($replyIdMap){
            $replyList = Db::name('comment')->where(['id'=>['in', $replyIdMap]])->select();
            foreach ($list as $key => $val){
                foreach ($replyList as $item){
                    if($val['reply_id'] == $item['id']){
                        $list[$key]['reply'] = $item;
                    }
                }
            }
        }
        return Response::success('ok', $list);
    }

}